package org.larkdoc.config;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.larkdoc.auth.JsonFormAuthenticationFilter;
import org.larkdoc.auth.UserShiroRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * shiro 权限控制配置
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Configuration
public class ShiroConfig {
    
    private final static Logger LOG = LoggerFactory.getLogger(ShiroConfig.class);
    
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        LOG.info("ShiroConfig shiroFilter start.");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 必须设置SecuritManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 拦截器  
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //项目主界面设置为可匿名访问
        filterChainDefinitionMap.put("/", "anon");
        //静态界面
        filterChainDefinitionMap.put("/static/**", "anon");
        // 用户注册
        filterChainDefinitionMap.put("/user/register", "anon");
        filterChainDefinitionMap.put("/logout", "anon");
        //filterChainDefinitionMap.put("/login_pre", "anon");
        //filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/loginAjax", "anon");
        filterChainDefinitionMap.put("/unauthorized", "anon");
        filterChainDefinitionMap.put("/demo/**", "anon");
        filterChainDefinitionMap.put("/home/**", "anon");
        filterChainDefinitionMap.put("/isLogin", "anon");
        // 开放文档查询权限
        filterChainDefinitionMap.put("/doc/get/**", "anon");
        filterChainDefinitionMap.put("/docRelease/get/**", "anon");
        filterChainDefinitionMap.put("/docRelease/history/**", "anon");
        //md文档图片查看权限，放开
        filterChainDefinitionMap.put("/file/get/mdImg/**", "anon");
        // 登录界面设置为主界面 index.html
        shiroFilterFactoryBean.setLoginUrl("/static/html/login.html");
        // 未授权界面,返回主界面;  
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized.html");
        // 过滤链定义，从上向下顺序执行，一般将 /**放在最为下边  ,因为匹配顺序的问题 
        // authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问 
        // Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        // 自定义url权限判断过滤器
        //        filters.put("url-perms", new UrlPersAuthorizationFilter());
        // 目前只做登录权限控制：
        //filterChainDefinitionMap.put("/**", "authc,url-perms");
        filterChainDefinitionMap.put("/**", "authc");
        
        // 自定义过滤器
        //Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        //        filters.put("json-authc", jsonFilter());
        //        filterChainDefinitionMap.put("/**", "json-authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        LOG.info("ShiroConfig shiroFilter end.");
        return shiroFilterFactoryBean;
    }
    
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 注入自定义的realm
        securityManager.setRealm(realm());
        return securityManager;
    }
    
    @Bean
    public Realm realm() {
        return new UserShiroRealm();
    }
    
    public FormAuthenticationFilter jsonFilter() {
        // 如果加入@Bean 注解，会把Filter加入到 spring的拦截链中，造成/css/**=anon, /js/**=anon 被拦截
        return new JsonFormAuthenticationFilter();
    }
}
